-------------------------------------------------------------------------------
--	 File name: init.lua
--		Author: Sploder
-- 		  Date: 03/10/2010
-- Description: This file contains the main body of code used by Sakuyas's 
--					special knives used when the player uses spellcard "Killing
--					Doll."
--
-------------------------------------------------------------------------------

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')

ENT.shotspeed	= 2000		-- The speed of the knife that comes afterwards...

-------------------------------------------------------------------------------
--	  Function: ENT:PhysicsUpdate()
-- Description: This function is called when we need to update the physics of
--					the entity.
-------------------------------------------------------------------------------
function ENT:PhysicsUpdate()
	if (SERVER)
	then
		-- Spin the knife.
		self.angle = self.angle + self.angleSpeed
		-- Set the new angle.
		self:SetLocalAngles(Angle(-self.angle, self:GetLocalAngles().y, 0))
		-- Slow the knife's velocity down.
		self:GetPhysicsObject():SetVelocity(self:GetPhysicsObject():GetVelocity() * 0.99)
	end
end

-------------------------------------------------------------------------------
--	  Function: ENT:OnTakeDamage()
-- Description: This function is called when the entity takes damage.
-------------------------------------------------------------------------------
function ENT:OnTakeDamage()
	-- Don't do anything when the entity takes damage.
end

-------------------------------------------------------------------------------
--	  Function: ENT:PhysicsCollide(data, physobj)
-- Description: This function is called when the entity collides with
--					something.
-------------------------------------------------------------------------------
function ENT:PhysicsCollide(data, physobj)
	-- Bounce off of anything we can collide with (pretty much just the world...)
	
	-- Make noise.
	WorldSound("sakuya/knifeBounce.wav", self:GetPos(), 75, 100)

	-- Find the reflection of the old velocity off of the hit normal
	local number = -2 * data.OurOldVelocity:Dot(data.HitNormal)
	local NewVelocity = data.HitNormal * number
	NewVelocity = data.OurOldVelocity + NewVelocity
	-- Reset the new velocity's speed to be the same as it was before the collision.
	NewVelocity:Normalize()
	NewVelocity = NewVelocity * data.OurOldVelocity:Length()
	-- Set the knife's new velocity.
	physobj:SetVelocity(NewVelocity)
			

end

-------------------------------------------------------------------------------
--	  Function: ENT:StartTouch()
-- Description: This function is called when something first touches the
--					entity.
-------------------------------------------------------------------------------
function ENT:StartTouch()
end

-------------------------------------------------------------------------------
--	  Function: ENT:EndTouch()
-- Description: This function is called when something stops touching the
--					entity.
-------------------------------------------------------------------------------
function ENT:EndTouch()
end

-------------------------------------------------------------------------------
--	  Function: ENT:Touch(hitent)
-- Description: This function is called when something is touching the entity.
-------------------------------------------------------------------------------
function ENT:Touch(hitent)
	-- Don't do anything when we touch stuff either...
end

-------------------------------------------------------------------------------
--	  Function: ENT:OnRemove()
-- Description: This function is called when the entity is removed.
-------------------------------------------------------------------------------
function ENT:OnRemove()
end

-------------------------------------------------------------------------------
--	  Function: ENT:Think()
-- Description: This function is called every step (or so) to do stuff...
-------------------------------------------------------------------------------
function ENT:Think()
	-- Set the next think to be the next frame.
	self:NextThink(CurTime())
	
	-- If the knife has been around past the kill timer, kill it and create a new knife to move towards the nearest player.
	if (CurTime() > self.timer)
	then	
			-- Make noise?
			WorldSound("sakuya/knifeThrow03.wav", self:GetPos(), 100, 100)
			
			-- Set the default target to be the user's crosshair.
			local target = self.Owner:GetEyeTrace().HitPos
			local aimvector = target - self:GetPos()
			aimvector:Normalize()
			
			-- If there is a player close to the crosshair, target the closest one to the crosshair.
			
			-- Create the projectile and aim it for the target.
			
			-- Set up the projectile.
			local proj = ents.Create("sent_proj_sakuyaknife01")
			proj.Owner = self.Owner
			
			-- Find the position to shoot the knife from.
			proj:SetPos(self:GetPos())
			
			-- Find the angle at which to shoot the knife.
			proj:SetAngles(aimvector:Angle())
			
			-- Spawn it.
			proj:Spawn()
			
			-- Set some physics properties.
			proj:GetPhysicsObject():Wake()
			proj:GetPhysicsObject():EnableDrag(false)
			proj:GetPhysicsObject():EnableGravity(false)
			proj:GetPhysicsObject():SetVelocity(aimvector * self.shotspeed)
			proj:GetPhysicsObject():SetBuoyancyRatio(0)
			
			util.SpriteTrail(proj, 0, Color(127, 0, 0, 200), true, 8, 2, 0.5, 0.05, "trails/sakuya/knifeTrail.vmt")
			proj:SetMaterial("models/danmaku/sakuya/RKnife2")
			
			proj.damage = self.damage
			proj.timer = CurTime() + 2.5
			proj.pPerHit = self.pPerHit
			proj.pPerKill = self.pPerKill
			
			-- Remove this entity.
			self:Remove()
	end

	
	return true
end